import { IAddressType } from "./../store/address";
import { ICartType } from "../store/cart";
import request from "../utils/request";
export interface IUserType {
  username: string;
  password: string;
}
export const userReg = (user: IUserType) => {
  return request.post("/users", user);
};

export const userLogin = (user: IUserType) => {
  return request.post("/login", user);
};

//新增购物车
export const cartPost = (cartObj: ICartType) => {
  return request.post("/classes/VueCart", cartObj);
};

//更新购物车
export interface IUpdateCartParams {
  num?: number;
  ischeck?: boolean;
}
export const cartPut = (cartid: string, updateObj: IUpdateCartParams) => {
  return request.put(`/classes/VueCart/${cartid}`, updateObj);
};

//根据用户id,请求购物车数据
export const cartGet = (userid: string) => {
  return request.get("/classes/VueCart", {
    params: {
      where: { userid: userid },
    },
  });
};

// 删除购物车
export const cartDel = (cartid: string) => {
  return request.delete(`/classes/VueCart/${cartid}`);
};

// 批量全选更新
export const cartBatch = (cartlist: Array<ICartType>, allCheck: boolean) => {
  const requests = cartlist.map((item) => {
    return {
      method: "PUT",
      path: `/1.1/classes/VueCart/${item.objectId}`,
      body: {
        ischeck: allCheck,
      },
    };
  });
  return request.post(`/batch`, { requests });
};

//新增地址
export const addressPost = (addressObj: IAddressType) => {
  return request.post("/classes/VueAddress", addressObj);
};

//根据用户id,请求地址列表数据
export const addressGet = (userid: string) => {
  return request.get("/classes/VueAddress", {
    params: {
      where: { userid: userid },
    },
  });
};

export const addressPut = (addressId: string, addressObj: IAddressType) => {
  return request.put(`/classes/VueAddress/${addressId}`, addressObj);
};
